/**
 * 发票来源详情
 */
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { messages } from "share/common";
import { Form, Radio } from 'antd'
import SlideFrameContent from '../slide-frame-content'
import settingService from '../request-and-expense-settings.service'

const RadioGroup = Radio.Group;

class NewInvoiceSource extends Component {

  record = this.props.params.record;

  state = {
    alipayTransactionIsHide: this.record.receiptSourceOptId ? !this.record.alipayInvoice : false,
  };

  radioOptions = [
    {label: messages('expense-2.key18')/*开启*/, value: true},
    {label: messages('expense-2.key19')/*关闭*/, value: false},
  ];

  radioOptions1 = [
    {label: messages('common.yes')/*是*/, value: true},
    {label: messages('common.no')/*否*/, value: false}
  ];

  getCompanies = () => {
    let companies = [];
    this.record.companies && this.record.companies.map((id, index) => {
      companies.push({ id, name: this.record.companyNameList[index] })
    });
    return companies;
  };

  priorityInfo = {
    top: (this.props.authPolicyAll || this.record.receiptSourceOptId) ? 'TENANT' : 'COMPANY',
    disabled: !!this.record.receiptSourceOptId,
    value: this.record.receiptSourceOptId ? {
      levelCode: this.record.levelCode,
      levelOrgId: this.record.levelOrgId,
      levelOrgName: this.record.levelOrgName,
      distribution: {
        companies: this.getCompanies()
      }
    } : {
      levelCode: this.props.authPolicyAll ? 'TENANT' : 'COMPANY',
      levelOrgId: this.props.authPolicyAll ? this.props.tenant.id : this.props.company.id,
      levelOrgName: this.props.authPolicyAll ? this.props.tenant.tenantName : this.props.company.name,
    }
  };

  formList = [{
    id: 'electronicInvoice',
    label: messages('expense-2.key20')/*微信导入电子票*/,
    extra: messages('expense-2.key21')/*配置为“开启”，可在账本和报销单中导入微信卡包中的电子发票*/,
    initialValue: this.record.receiptSourceOptId ? this.record.electronicInvoice : true,
    component: <RadioGroup disabled={this.record.readOnly} options={this.radioOptions}/>
  }, {
    id: 'alipayInvoice',
    label: messages('expense-2.key22')/*支付宝导入发票*/,
    extra: messages('expense-2.key23')/*配置为“开启”，可在账本和报销单中导入支付宝发票管家中的发票*/,
    initialValue: this.record.receiptSourceOptId ? this.record.alipayInvoice : true,
    component: (
      <RadioGroup
        disabled={this.record.readOnly}
        options={this.radioOptions}
        onChange={e => this.setState({ alipayTransactionIsHide: !e.target.value })}
      />
    )
  }, {
    id: 'alipayTransaction',
    label: messages('expense-2.key24')/*是否导入匹配的支付宝账单记录*/,
    extra: messages('expense-2.key25')/*导入支付宝发票时，系统在支付宝账单中匹配此发票对应的消费记录，若匹配成功，则会将消费记录带入发票关联的费用中；若未匹配成功，则不带入*/,
    initialValue: this.record.receiptSourceOptId && this.record.alipayInvoice ? this.record.alipayTransaction : true,
    component: <RadioGroup disabled={this.record.readOnly} options={this.radioOptions1}/>
  }, {
    id: 'emailInvoice',
    // label: messages('expense-2.key26')/*邮箱导入发票*/,
    // extra: messages('expense-2.key27')/*配置为“开启”，可在账本和报销单中导入汇联易邮箱中的发票*/,
    // 发票夹设置配置页面文件修改 9125
    label: messages('expense-2.key28')/*发票夹与邮箱导入发票*/,
    extra: messages('expense-2.key29')/*配置为“开启”，可在账本和报销单中导入微信小程序“我的发票夹”中，或导入汇联易邮箱中的发票*/,
    initialValue: this.record.receiptSourceOptId ? this.record.emailInvoice : true,
    component: <RadioGroup disabled={this.record.readOnly} options={this.radioOptions}/>
  }, {
    id: 'scanningInvoice',
    label: messages('expense-2.key30')/*扫发票二维码*/,
    extra: messages('expense-2.key31')/*配置为“开启”，可在账本和报销单中扫发票二维码归集发票*/,
    initialValue: this.record.receiptSourceOptId ? this.record.scanningInvoice : true,
    component: <RadioGroup disabled={this.record.readOnly} options={this.radioOptions}/>
  }, {
    id: 'handInvoice',
    label: messages('expense-2.key32')/*手录发票*/,
    extra: messages('expense-2.key33')/*配置为“开启”，可在账本和报销单中手录发票生成费用，也可在费用详情页手录发票*/,
    initialValue: this.record.receiptSourceOptId ? this.record.handInvoice : true,
    component: <RadioGroup disabled={this.record.readOnly} options={this.radioOptions}/>
  }];

  dealParams = (values) => {
    values.receiptSourceOptId = this.record.receiptSourceOptId;
    !values.alipayInvoice && (values.alipayTransaction = false);
    return values;
  };

  render() {
    const { alipayTransactionIsHide } = this.state;
    return (
      <SlideFrameContent
        disabled={this.record.readOnly}
        enabled={this.record.receiptSourceOptId ? this.record.enabled : true}
        priorityInfo={this.priorityInfo}
        formInfo={[
          {
            title: messages('expense-2.key34')/*发票来源设置*/,
            list: this.formList.map(item => {
              item.id === 'alipayTransaction' && (item.hide = alipayTransactionIsHide);
              return item;
            })
          }
        ]}
        service={settingService.updateInvoiceSource}
        dealParams={this.dealParams}
        close={this.props.close}
      />
    )
  }

}

function mapStateToProps(state) {
  return {
    authPolicyAll: state.main.authPolicyAll,
    company: state.login.company,
    tenant: state.login.tenant,
  }
}

const WrappedNewInvoiceSource = Form.create()(NewInvoiceSource);

export default connect(mapStateToProps)(WrappedNewInvoiceSource);
